package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.dingreading.cloud.soms.dto.MstXcxStudentLearningProgressDto;
import com.dingreading.cloud.soms.entity.MstXcxStudentLearningProgress;
import com.dingreading.cloud.soms.entity.table.MstXcxStudentLearningProgressTableDef;
import com.dingreading.cloud.soms.mapper.MstXcxStudentLearningProgressMapper;
import com.dingreading.cloud.soms.service.MstXcxStudentLearningProgressService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import static com.dingreading.cloud.soms.entity.table.MembersTableDef.members;

/**
 * 小程序学生学习进度表 服务层实现。
 *
 * @author EDY
 * @since 2025-11-03
 */
@Service
public class MstXcxStudentLearningProgressServiceImpl extends BaseServiceImpl<MstXcxStudentLearningProgressMapper, MstXcxStudentLearningProgress, MstXcxStudentLearningProgressTableDef> implements MstXcxStudentLearningProgressService {

    @Override
    protected MstXcxStudentLearningProgressTableDef getTable() {
        return MstXcxStudentLearningProgressTableDef.mstXcxStudentLearningProgress;
    }

    @Override
    public Page<MstXcxStudentLearningProgressDto> pageList(
            PageUtil pageUtil, String corpUid, String storeUid, String staffUid, String staffName,
            String keyword, Integer materialStage, String subjectUid, String beginDate, String endDate
    ) {
        QueryCondition condition = QueryCondition.createEmpty()
                .and(table.agencyUid.eq(corpUid))
                .and(table.storeUid.eq(storeUid));

        if (StringUtils.isNotBlank(keyword))
            condition.and(members.memberName.like(keyword).or(members.mob.like(keyword)));
        if (StringUtils.isNotBlank(subjectUid))
            condition.and(table.projectUid.eq(subjectUid));
        if (materialStage != null)
            condition.and(table.materialStage.eq(materialStage));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));

        QueryWrapper wrapper = QueryWrapper.create()
                .leftJoin(members).on(table.studentUid.eq(members.uid))
                .where(condition)
                .orderBy(table.id.desc());

        Page<MstXcxStudentLearningProgressDto> progressDtoList = getMapper().paginateWithRelationsAs(
                Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, MstXcxStudentLearningProgressDto.class
        );
        return progressDtoList;
    }
}
